package com.zzyl.nursing.service.impl;

import java.util.List;

import com.zzyl.common.constant.Constants;
import com.zzyl.common.core.domain.model.LoginUser;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.framework.web.service.TokenService;
import com.zzyl.nursing.dto.QueryAlertDataDto;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Map;

/**
 * 报警数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-25
 */
@Slf4j
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper,AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;

    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     * 
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Autowired
    TokenService tokenService;
    @Override
    public List<AlertData> selectAlertDataList(QueryAlertDataDto dto)
    {
        //要求只有接收人才能查看,所以获取当前对象的用户id
        log.info("当前用户:"+dto.getUserId());
        //前端没传,自己获取
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if(loginUser==null){
            log.info("当前用户为空");
            return null;
        }
        Long userId = loginUser.getUserId();
        dto.setUserId(userId);
        //通过用户id获取roleId
        Long[] roleIds = alertDataMapper.selectRoleIdByUserId(userId);
        int status=0;
        for(Long roleId : roleIds){
            if(roleId==1){
                log.info("当前用户是超级管理员");
                status=1;
            }
        }
        if(status==1){
            return alertDataMapper.selectAlertDataListAll(dto);
        }
        return alertDataMapper.selectAlertDataList(dto);
    }


    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return save(alertData)?1:0;
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData)?1:0;
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids))?1:0;
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id)?1:0;
    }
    @Override
    public Integer handleAlertData(AlertData alertData) {
        //根据id,设置数据库对应数据的status,处理结果,处理时间等
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //处理人
        String username = loginUser.getUsername();
        alertData.setProcessorName(username);
        //处理人id
        alertData.setUpdateBy(String.valueOf(loginUser.getUserId()));
        //处理结果
        alertData.setProcessingResult(alertData.getProcessingResult());
        //状态
        alertData.setStatus(1);
        //处理时间
        alertData.setUpdateTime(DateUtils.getNowDate());
        //获取警报数据,这里因为不能只通过id修改
        //因为多个护工收到的数据id不一样,但是我们需要把全部都修改了
        //这里我选择用设备名作为索引删除
        AlertData alertData1 = alertDataMapper.selectAlertDataById(alertData.getId());
        alertData.setDeviceName(alertData1.getDeviceName());
        alertData.setCreateTime(alertData1.getCreateTime());
        Integer result = alertDataMapper.handleAlertData(alertData);
        return result;
    }
}
